// src/stores/authStore.js
import { defineStore } from 'pinia';
import LocalStorageUtil from '@/utils/localStorageUtil';
export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: null,
    username: null,
    idCard:null,
    id:null,
    registrationStatus:false,

  }),
  actions: {
    referenceStatus(){
      this.username = LocalStorageUtil.getLocalStorage("username");
      this.idCard=LocalStorageUtil.getLocalStorage("idCard");
      this.id=LocalStorageUtil.getLocalStorage("id");
      this.token = LocalStorageUtil.getLocalStorage("token");
    },

    login(userInfo) {
      this.username = userInfo.pName;
      this.idCard=userInfo.pCard
      this.id=userInfo.pId;
      this.token = userInfo.token;
      
      LocalStorageUtil.setLocalStorage("username", this.username);
      LocalStorageUtil.setLocalStorage("idCard", this.idCard);
      LocalStorageUtil.setLocalStorage("id", this.id);
      LocalStorageUtil.setLocalStorage("token", this.token);
      
    },
    setRegistrationStatus(){
      this.registrationStatus=!this.registrationStatus;
    },
    logout() {
      LocalStorageUtil.removeLocalStorage("username");
      LocalStorageUtil.removeLocalStorage("idCard");
      LocalStorageUtil.removeLocalStorage("id");
      LocalStorageUtil.removeLocalStorage("token");
      // 清除 token 从 cookie
     
    },
  },
  getters: {
    isAuthenticated: (state) => !!state.token,
    getUsername: (state) => state.username,
    getIdCard: (state) => state.idCard,
    getId: (state) => state.id,
    getRegistrationStatus:(state)=>{state.registrationStatus}
  },
});